//
//  SettingViewController.m
//  Ufan
//
//  Created by yangqingluo on 16/1/15.
//  Copyright © 2016年 yangqingluo. All rights reserved.
//

#import "SettingViewController.h"
#import "EMSDImageCache.h"
#import "SettingPushCell.h"

@interface SettingViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (strong, nonatomic) NSArray *showArray;
@property (strong, nonatomic) UITableView *showTableView;

@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGBA(245, 245, 245, 1.0);
    [self setupBarButtonItem];
    
    [self.view addSubview:self.showTableView];
}

- (void)setupBarButtonItem{
    self.title = @"设置";
    
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 64, 44)];
    [backButton setImage:[UIImage imageNamed:@"nav_back.png"] forState:UIControlStateNormal];
    [backButton setImageEdgeInsets:UIEdgeInsetsMake(12, 0, 12, 48)];
    backButton.tag = 988;
    [backButton addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    [self.navigationItem setLeftBarButtonItem:backItem];
}

-(void)goBack{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma getter
- (UITableView *)showTableView{
    if (!_showTableView) {
        _showTableView= [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height) style:UITableViewStyleGrouped];
        _showTableView.multipleTouchEnabled = YES;
        _showTableView.dataSource = self;
        _showTableView.delegate = self;
//        _showTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_showTableView setBackgroundColor:[UIColor whiteColor]];
        _showTableView.sectionHeaderHeight = 0;
    }
    
    return _showTableView;
}

- (NSArray *)showArray{
    if (!_showArray) {
        _showArray = @[@{@"title":@"推送设置",@"subTitle":@"",@"headImage":@"account_00"},
                       @{@"title":@"清除图片缓存",@"subTitle":@"",@"headImage":@"account_01"},];
    }
    
    return _showArray;
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.showArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        static NSString *CellIdentifier0 = @"SettingCell0";
        SettingPushCell *cell = (SettingPushCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier0];
        
        if (cell == nil) {
            cell = [[SettingPushCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier0];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.textLabel.font = [UIFont systemFontOfSize:16.0];
        }
        
        
        NSDictionary *dic = self.showArray[indexPath.row];
        cell.textLabel.text = dic[@"title"];

        
        return cell;
    }
    else if (indexPath.row == 1){
        static NSString *CellIdentifier1 = @"SettingCell1";
        UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
        
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier1];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.textLabel.font = [UIFont systemFontOfSize:16.0];
            
            cell.detailTextLabel.font = cell.textLabel.font;
            cell.detailTextLabel.textColor = cell.textLabel.textColor;
        }
        
        
        NSDictionary *dic = self.showArray[indexPath.row];
        cell.textLabel.text = dic[@"title"];
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2fM",[EMSDImageCache sharedImageCache].getSize / 1024.0 / 1024.0];
        
        return cell;
    }
    
    
    return [UITableViewCell new];
}


#pragma mark - Table view delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
            
        case 0:{

        }
            break;

        case 1:{
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"确定清除缓存？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alert show];
        }
            break;
            

            
        default:
            break;
    }
}

#pragma alert
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        //清除图片缓存
        
        [self showHudInView:self.view hint:nil];
        [[EMSDImageCache sharedImageCache]clearDiskOnCompletion:^{
            [self hideHud];
            [self.showTableView reloadData];
        }];
    }
    
}

@end
